//
//  SortViewController.swift
//  SwiftDemo
//
//  Created by Jason on 15/10/30.
//  Copyright © 2015年 Jason Jiang. All rights reserved.
//

import UIKit

class SortViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        
        
/// 老的sort 方法
        
        
//        let  cmptr:NSComparator = {(obj1:AnyObject ,obj2:AnyObject) in
//            
//            
//            if(obj1.integerValue > obj2.integerValue){
//                
//                return NSComparisonResult.OrderedDescending
//            }
//            
//            
//            if(obj1.integerValue < obj2.integerValue){
//                
//                return NSComparisonResult.OrderedAscending
//            }
//            
//            if(obj1.integerValue == obj2.integerValue){
//                
//                return NSComparisonResult.OrderedSame;
//            }
//            
//            return NSComparisonResult.OrderedSame;
//        }
        
        
        var array:[Int] = [1,3,4,5,3,7,4,32,7,9];
        
        NSLog("数组数据是%@", array);

      
        
        array.sortInPlace({$0 < $1})
        
        NSLog("第一次 sort 数组数据是%@", array);
        
        
        
        
        var images:[imageFile] = [];
        
        for (var i = 0 ; i < 6; i++){
            
            let img:imageFile = imageFile()
            img.fileID = 5 - i;
            img.fileName = "fimeName" + String(i);
            images.append(img);
        }
        
        
        for (var i = 0 ; i < 6; i++){
            
            NSLog(" sort 数组数据是%d", images[i].fileID);
            
        }
        
        
        

        
        //一般形式
        
        images.sortInPlace { (image1:imageFile, image2:imageFile) -> Bool in
            
            return image1.fileID < image2.fileID;
        }
        
        for (var i = 0 ; i < 6; i++){
            
            NSLog("第一次 sort 数组数据是%d", images[i].fileID);
            
        }
        
        //省略-> 形式
        
        images.sortInPlace { (image1, image2)  in
            
            return image1.fileID < image2.fileID
        }
        
        // 带返回值的简单闭包形式
        
        images.sortInPlace { (image1, image2) in
            
            image1.fileID < image2.fileID;
        }
        
        //隐含参数形式
        images.sortInPlace({$0.fileID > $1.fileID})
        
        
        for (var i = 0 ; i < 6; i++){
            
            NSLog("第一次 sort 数组数据是%d", images[i].fileID);
            
        }
        
  
    }
    

    
    
        
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}


class imageFile {
    
    var fileName = String();
    var fileID = Int();
    
}

